package ruifight.member.controller;

import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import ruifight.member.model.IntegralRecord;
import ruifight.member.model.IntegralRecordExample;
import ruifight.member.model.MemberInfo;
import ruifight.member.service.IntegralRecordService;
import ruifight.member.service.MemberInfoService;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/interface")
public class IntegralRecordController {

    private static final Logger logger = LoggerFactory.getLogger(IntegralRecordController.class);

    @Autowired
    public IntegralRecordService integralRecordService;
    @Autowired
    public MemberInfoService memberInfoService;

    /**
     * 会员积分增加/消费记录
     * @param paramMap
     * @return
     */
    @RequestMapping("/addIntegralRecord")
    @ResponseBody
    public Map<String, Object> addIntegralRecord(@RequestBody Map<String, String> paramMap){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            MemberInfo memberInfo = new MemberInfo();
            memberInfo.setMemberInfoId(Long.parseLong(paramMap.get("memberInfoId")));
            if(Integer.parseInt(paramMap.get("integralType")) == 1){
                memberInfo.setMemberIntegral(Integer.parseInt(paramMap.get("memberIntegral")) - Integer.parseInt(paramMap.get("integral")));
            }else{
                memberInfo.setMemberIntegral(Integer.parseInt(paramMap.get("memberIntegral")) + Integer.parseInt(paramMap.get("integral")));
            }
            memberInfoService.updateByPrimaryKeySelective(memberInfo);

            IntegralRecord integralRecord = new IntegralRecord();
            integralRecord.setMemberInfoId(Long.parseLong(paramMap.get("memberInfoId")));
            integralRecord.setIntegralType(Integer.parseInt(paramMap.get("integralType")));
            integralRecord.setIntegral(Integer.parseInt(paramMap.get("integral")));
            integralRecord.setIntegralComment(paramMap.get("integralComment"));
            integralRecord.setCreateDate(new Date());
            integralRecordService.insertSelective(integralRecord);
            map.put("state",1);
            map.put("msg","提交成功");
        } catch (Exception e){
            map.put("state",0);
            map.put("msg","提交失败");
            e.printStackTrace();
        }
        return map;
    }

    //通过会员Id获取积分记录
    @RequestMapping("/getIntegralRecordList")
    @ResponseBody
    public Map<String, Object> getIntegralRecordList(@RequestBody Map<String, String> paramMap){
        Map<String, Object> map = new HashMap<String, Object>();
        String cur_page= paramMap.get("cur_page").toString();
        String page_size= paramMap.get("page_size").toString();
        PageInfo<IntegralRecord> pageInfo = integralRecordService.getIntegralRecordList(Integer.parseInt(cur_page), Integer.parseInt(page_size), paramMap);
        map.put("rows", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return map;
    }

    //获取所有会员积分记录
    @RequestMapping("/getIntegralRecordAllList")
    @ResponseBody
    public Map<String, Object> getIntegralRecordAllList(@RequestBody Map<String, String> paramMap){
        Map<String, Object> map = new HashMap<String, Object>();
        String cur_page= paramMap.get("cur_page").toString();
        String page_size= paramMap.get("page_size").toString();
        PageInfo<IntegralRecord> pageInfo = integralRecordService.getIntegralRecordAllList(Integer.parseInt(cur_page), Integer.parseInt(page_size), paramMap);
        int consumptionIntegral = integralRecordService.getConsumptionIntegral(paramMap);
        int surplusIntegral = memberInfoService.getSurplusIntegral();
        map.put("rows", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        map.put("consumptionIntegral",consumptionIntegral);
        map.put("surplusIntegral",surplusIntegral);
        return map;
    }

}
